package com.v3admin.system.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.v3admin.common.controller.IResponse;
import com.v3admin.system.domain.vo.UserLoginVO;
import com.v3admin.system.domain.vo.UserTokenVO;
import com.v3admin.system.service.UserLoginService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 用户登录
 *
 * @author Junpeng.Li
 * @date 2023-12-13 15:40
 */
@Validated
@RestController
@AllArgsConstructor
public class UserLoginController {

    private final UserLoginService userLoginService;

    /**
     * 用户登录
     *
     * @param loginInfo 登录信息
     * @return 登录结果
     */
    @SaIgnore
    @PostMapping("/login")
    public IResponse login(@RequestBody @Valid UserLoginVO loginInfo) {
        UserTokenVO token = userLoginService.login(loginInfo);
        return IResponse.success("登录成功", token);
    }

    /**
     * 验证当前登录用户是否已登录
     *
     * @return 登录状态
     */
    @GetMapping("/isLogin")
    public IResponse isLogin() {
        return IResponse.success(StpUtil.isLogin() ? "已登录" : "未登录");
    }

    /**
     * 退出登录
     *
     * @return 操作结果
     */
    @PostMapping(value = "/logout")
    public IResponse logout() {
        StpUtil.logout();
        return IResponse.success("用户退出登录成功");
    }

}
